package api

import (
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"oidc/dao"
	"oidc/model"
)

func RegisterClient() gin.HandlerFunc {
	return func(c *gin.Context) {
		status := false
		var client model.Client
		clientid := dao.HandUUID(uuid.New().String())
		clientsc := dao.HandUUID(uuid.New().String())
		name := c.Query("name")
		RUrl := c.Query("url")
		for _, val := range dao.UserMap {
			if val.UserName == name {
				status = true
				client.Order = val.Identifier
			}
		}
		if !status {
			c.JSON(200, gin.H{
				"status": status,
				"data":   "没有此用户",
			})
			return
		}

		client.ClientID = clientid
		client.ClientSecret = clientsc
		client.RedirectUrl = RUrl
		dao.AddClient(client)
		c.JSON(200, client)
	}
}
